import os
import cv2
import numpy as np
from common.config import config_option

opencv_haarcascades_file = "D:\\directory\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_default.xml"

# 加载训练数据
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read(config_option['project_path'] + "/output/face_recognition/train_face_recognition.yml")

# 准备识别图片
img = cv2.imread(config_option['project_path'] + "/data/face_recognition/1.pgm")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
detector = cv2.CascadeClassifier(opencv_haarcascades_file)
faces = detector.detectMultiScale(gray)
for (x, y, w, h) in faces:
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)
    # 人脸识别
    pre_id, pre_confidence = recognizer.predict(gray[y:y + h, x:x + w])
    print("标签ID", pre_id, "置信评分", pre_confidence)
cv2.imshow('result', img)
cv2.waitKey()
cv2.destroyAllWindows()
